
#include <iostream>
#include <mysql/mysql.h>
#include "smallcardb.h"
#include "odom_slam.h"
#include "odom_gps.h"
#include "odom_imu.h"
#include <list>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <functional>
using namespace std;
/*---------------------------------数据操作类---------------------------------------*/


smallcardb::smallcardb():
conn(NULL),
res(NULL),
row(NULL)
{
   
     cout<<"buld smallcar_db success\r\n"<<endl; 
}

smallcardb::~smallcardb(){

      if(conn!=NULL){
        mysql_close(conn);
      }
      cout<<"destroy smallcar_db success\r\n"<<endl;
}


void  smallcardb::car_db_init(){

    //char server[] = "lintengbo.local";
    char server[] = "localhost";
    char user[] = "root";
    char password[] = "ubuntu";
    char database[] = "smallcar";
    char ins_slam[240];
    char ins_gps[240];
    char ins_imu[240];
    conn = mysql_init(NULL);
   
    if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }


    sprintf(ins_slam,  " create table if  not  exists odom_slam(time_stamp BIGINT,"
                 "slam_angle FLOAT,  slam_distance INT,  slam_x_axes  FLOAT,  "
                 " slam_y_axes FLOAT)");
    if(mysql_query(conn, ins_slam))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins_slam);
    }

    sprintf(ins_gps,  " create table if  not  exists odom_gps(time_stamp BIGINT,"
                 "  gps_latitude FLOAT,  gps_longitude  FLOAT, gps_high FLOAT,gps_angle FLOAT, "
                 " gps_x_axes FLOAT ,gps_y_axes FLOAT)");
    if(mysql_query(conn, ins_gps))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins_gps);
    }

    sprintf(ins_imu,  " create table if  not  exists odom_imu(time_stamp BIGINT,"
                 "imu_angle FLOAT, imu_distance INT,  imu_x_axes  FLOAT,  "
                 " imu_y_axes FLOAT)");
    if(mysql_query(conn, ins_imu))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins_imu);
    }
}

MYSQL* smallcardb::get_mysql_conn(){

      return  conn;
}

void  smallcardb::set_mysql_conn(MYSQL* my_conn){

      conn=my_conn;
}

MYSQL_RES* smallcardb::get_mysql_res(){

      return  res;
}

void  smallcardb::set_mysql_res(MYSQL_RES* my_res){

      res=my_res;
}



MYSQL_ROW  smallcardb::get_mysql_row(){

      return row;

}


void  smallcardb::set_mysql_row(MYSQL_ROW my_row){

      row=my_row;
}

/*----------------------------------odom_slam 增加删除修改-----------------*/
void  smallcardb::Add_odom_slam(odom_slam *Odom_Slam)
{
    char ins[240];
    sprintf(ins, "insert into odom_slam (time_stamp, "
                 "slam_angle,slam_distance,slam_x_axes,slam_y_axes) "
                 "values('%d', '%f', '%d', '%f', '%f')",Odom_Slam->get_time_stamp(), Odom_Slam->get_angle(),Odom_Slam->get_distance(),Odom_Slam->get_x_axes(),Odom_Slam->get_y_axes());
    if(mysql_query(conn, ins))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins);
    }


 
}
void  smallcardb::Update_odom_slam(int timestamp){

     

  
}

void  smallcardb::Delete_odom_slam(float  start_x_axes,float end_x_axes)
{
    char ins[240];
    sprintf(ins, "delete   from   odom_slam  where slam_x_axes>='%f' "
                 "and slam_x_axes<='%f'" ,start_x_axes,end_x_axes);
    if(mysql_query(conn, ins))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins);
    }
}


list<odom_slam> smallcardb::getScroll_SlamData(float  start_x_axes,float end_x_axes)
{
    
     list<odom_slam>   list_odom_slam;//= new ArrayList<odom_slam>();// 创建集合对象

     MYSQL_RES *res;
     MYSQL_ROW row;  

     char ins[240];

     sprintf(ins, "select *  from   odom_slam"
                  "  where slam_x_axes>='%f' and slam_x_axes<='%f'" ,start_x_axes,end_x_axes);


     if(mysql_query(conn, ins))
     {
         fprintf(stderr, "%s\n", mysql_error(conn));
         fprintf(stderr, "%s\n", ins);
     }

     //mysql_store_result将全部的查询结果读取到
     res = mysql_store_result(conn);
     //mysql_fetch_row检索结果集的下一行
     while(row = mysql_fetch_row(res)) {

        odom_slam *OdomSlam=new odom_slam(atoi(row[0]),atof(
                    row[1]),atoi(row[2]),atof(row[3]),atof(row[4]));
        list_odom_slam.push_back(*OdomSlam);

     }   

     return  list_odom_slam;	  
	 
}
/*****************************************************************/


/*----------------------------------odom_gps 增加删除修改-----------------*/

void  smallcardb::Add_odom_gps(odom_gps *Odom_gps)
{
    char ins[240];
    sprintf(ins, "insert into odom_gps (time_stamp,gps_latitude,gps_longitude,gps_high,"
                 "gps_angle,gps_x_axes,gps_y_axes) values('%d', '%f', '%f', '%f', '%f','%f','%f')",
            Odom_gps->get_time_stamp(), Odom_gps->get_latitude(),Odom_gps->get_longitude(),
            Odom_gps->get_high(),Odom_gps->get_angle(),Odom_gps->get_x_axes(),Odom_gps->get_y_axes());

    if(mysql_query(conn, ins))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins);
    }
    cout<<"add odom_gpu success"<<endl;
}

void  smallcardb::Update_odom_gps(int timestamp){




}

void  smallcardb::Delete_odom_gps(float  start_x_axes,float end_x_axes)
{
    char ins[240];
    sprintf(ins, "delete   from   odom_gps  where gps_x_axes>='%f' and gps_x_axes<='%f'" ,start_x_axes,end_x_axes);
    if(mysql_query(conn, ins))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins);
    }

}

list<odom_gps> smallcardb::getScroll_GpsData(float  start_x_axes,float end_x_axes)
{

     list<odom_gps>   list_odom_gps;//= new ArrayList<odom_gps>();// 创建集合对象

     MYSQL_RES *res;
     MYSQL_ROW row;

     char ins[240];

     sprintf(ins, "select *  from   odom_gps  where gps_x_axes>='%f'"
                  " and gps_x_axes<='%f'" ,start_x_axes,end_x_axes);


     if(mysql_query(conn, ins))
     {
         fprintf(stderr, "%s\n", mysql_error(conn));
         fprintf(stderr, "%s\n", ins);
     }

     //mysql_store_result将全部的查询结果读取到
     res = mysql_store_result(conn);
     //mysql_fetch_row检索结果集的下一行
     while(row = mysql_fetch_row(res)) {

        odom_gps *Odomgps=new odom_gps(atoi(row[0]),atof(row[1])
                ,atoi(row[2]),atoi(row[3]),atoi(row[4]),atof(row[5]),atof(row[6]));
        list_odom_gps.push_back(*Odomgps);

     }

     return  list_odom_gps;

}
/*----------------------------------*******************************---------------*/


/*----------------------------------odom_imu 增加删除修改--------------------------------*/

void  smallcardb::Add_odom_imu(odom_imu *Odom_imu)
{
    char ins[240];
    sprintf(ins, "insert into odom_imu (time_stamp, imu_angle,imu_distance,"
                 "imu_x_axes,imu_y_axes)"
                 " values('%d', '%f', '%d', '%f', '%f')",Odom_imu->get_time_stamp(), Odom_imu->get_angle(),
            Odom_imu->get_distance(),Odom_imu->get_x_axes(),Odom_imu->get_y_axes());
    if(mysql_query(conn, ins))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins);
    }

}
void  smallcardb::Update_odom_imu(int timestamp){




}

void  smallcardb::Delete_odom_imu(float  start_x_axes,float end_x_axes)
{
    char ins[240];
    sprintf(ins, "delete   from   odom_imu  where imu_x_axes>='%f'"
                 " and imu_x_axes<='%f'" ,start_x_axes,end_x_axes);
    if(mysql_query(conn, ins))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        fprintf(stderr, "%s\n", ins);
    }
}


list<odom_imu> smallcardb::getScroll_ImuData(float  start_x_axes,float end_x_axes)
{

     list<odom_imu>   list_odom_imu;//= new ArrayList<odom_imu>();// 创建集合对象

     MYSQL_RES *res;
     MYSQL_ROW row;

     char ins[240];

     sprintf(ins, "select *  from   odom_imu  where "
                  "imu_x_axes>='%f' and imu_x_axes<='%f'" ,start_x_axes,end_x_axes);


     if(mysql_query(conn, ins))
     {
         fprintf(stderr, "%s\n", mysql_error(conn));
         fprintf(stderr, "%s\n", ins);
     }

     //mysql_store_result将全部的查询结果读取到
     res = mysql_store_result(conn);
     //mysql_fetch_row检索结果集的下一行
     while(row = mysql_fetch_row(res)) {

        odom_imu *Odomimu=new odom_imu(atoi(row[0]),atof(row[1]),atoi(row[2]),
                atof(row[3]),atof(row[4]));
        list_odom_imu.push_back(*Odomimu);

     }

     return  list_odom_imu;

}
/*****************************************************************/
